package com.guessnumber.guess;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

public class nogui implements CommandExecutor {
    private class eplayer {
        Player player;
        int number;
        int times;
        boolean is_playing;
        public void start(Player p){
            player = p;
            number = (int)(Math.random() * 100);
            is_playing = true;
            times = 1;
            player.sendMessage(ChatColor.GOLD + "开始猜数(0~99)！一人十次机会");
        }
        public void stop(){
            player.sendMessage(ChatColor.GOLD + "猜数已停止");
            is_playing = false;
            player = null;
            number = -1;
            times = -1;
        }
        public boolean need_to_stop(){
            return times > 10;
        }
        public void win(){
            player.sendMessage(ChatColor.GREEN + "猜对了！你一共猜了" + times + "次！");
            stop();
        }
        public void lose(){
            player.sendMessage(ChatColor.RED + "游戏结束！正确答案是" + number + "！");
            stop();
        }
        public void time(){
            player.sendMessage(ChatColor.BLUE + "现在是第" + times + "局");
        }
    }
    private class allplayer {
        ArrayList<eplayer> generic = new ArrayList<>();
        public int start(Player p){
            eplayer a = new eplayer();
            a.start(p);
            generic.add(a);
            return 1;
        }
        public int find_player(Player p){
            for (int i = 0;i < generic.size();i++){
                if (generic.get(i).player == p) return i;
            }
            return -1;
        }
        public boolean stop(Player p){
            int a = find_player(p);
            if (a == -1) return false;
            else {
                generic.get(a).stop();
                return true;
            }
        }
        public void bijiao(Player p,int n){
            int a = find_player(p);
            if (a == -1){
                p.sendMessage(ChatColor.RED + "查无此人！");
            }
            else {
                if (generic.get(a).number < n){
                    p.sendMessage(ChatColor.BLUE + "猜大了！");
                    generic.get(a).times += 1;
                    //return;
                } else if (generic.get(a).number > n){
                    p.sendMessage(ChatColor.BLUE +"猜小了！");
                    generic.get(a).times += 1;
                    //return;
                } else {
                    generic.get(a).win();
                    return;
                }
                if (generic.get(a).need_to_stop()){
                    generic.get(a).lose();
                }
            }
        }
    }
    //private eplayer[] players;
    private allplayer players = new allplayer();
    private void help(CommandSender sender){
        sender.sendMessage(ChatColor.BLUE +"""
                /guessnumber gui 猜数字（箱子界面版，未开发）
                /guessnumber start 开始猜数字
                /guessnumber stop 停止猜数字
                /guessnumber <数字> 猜一次(0~99)""");
    }
    @Override
    public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
        if (!(sender instanceof Player)){
            sender.sendMessage(ChatColor.RED + "该命令只能在玩家端使用！");
        }
        else {
            int argc = args.length;
            if ((argc == 0) || (argc == 1 && args[0].equals("help"))){
                help(sender);
            }
            else if (argc == 1 && args[0].equals("start")){
                int a = players.find_player((Player) sender);
                if (a != -1){
                    sender.sendMessage(ChatColor.RED + "你已经在猜数字了！");
                    return false;
                }
                int b = players.start((Player) sender);
                if (b == -1){
                    sender.sendMessage(ChatColor.BLUE +"服务器繁忙，请稍后重试");
                    return false;
                }
            } else if (argc == 1 && args[0].equals("stop")) {
                boolean a = players.stop((Player) sender);
                if (!a){
                    sender.sendMessage(ChatColor.RED + "查无此人！");
                    return false;
                }
               return true;
            } else if (argc == 1 && args[0].equals("is_playing")){
                //sender.sendMessage((players.find_player((Player) sender) == -1) ? "n" : "y");
                boolean a = players.find_player((Player) sender) == -1;
                if (!a) sender.sendMessage(ChatColor.BLUE + "你现在正在猜数字！");
                else sender.sendMessage(ChatColor.BLUE + "你现在未在猜数字！");
            }
            else {
                int a;
                try {
                    a = Integer.parseInt(args[0]);
                    players.bijiao((Player) sender,a);
                }
                catch (Exception e){
                    sender.sendMessage(ChatColor.RED + "数字不合法！");
                    return false;
                }

            }
        }
        return false;
    }
}
